<?php
/**
 * This file is form http://findcat.cn
 *
 * @link     https://findcat.cn
 * @email    1476982312@qq.com
 */
namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class UploadController
{
    /**
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response
     */
    public function upload(Request $request)
    {
        $file = $request->file('editormd-image-file');
        //判断文件是否上传成功
        if ($file->isValid()) {
            //原文件名
            $originalName = $file->getClientOriginalName();
            //扩展名
            $ext = $file->getClientOriginalExtension();
            //MimeType
            $type = $file->getClientMimeType();
            //临时绝对路径
            $realPath = $file->getRealPath();
            $filename = uniqid() . '.' . $ext;

            $bool = Storage::disk('qiniu')->put('images/articles/' . $filename, file_get_contents($realPath));

            //判断是否上传成功
            if ($bool) {
                return response()->json([
                    'success' => 1,
                    'message' => 'success',
                    'url'     => qiniu_url('images/articles/' . $filename),
                ]);
            }

            return response()->json([
                'success' => 0,
                'message' => 'fail',
                'url'     => null,
            ]);
        }
    }

    public function images()
    {
        //public/admin/uploads/images
    }

    public function files()
    {
    }
}
